<template>
	<view class="main-con">
		<cu-custom bgColor="bg-E96141" :isBack="true">
			<block slot="backText">返回</block>
			<block slot="content">会员健康档案代管</block>
		</cu-custom>
		<form>
			<view class="cu-form-group">
				<view class="title">账号</view>
				<input v-model="memberInfo.phone" disabled="disabled"></input>
			</view>
			<view class="cu-form-group">
				<view class="title">姓名</view>
				<input placeholder="(非必填)" name="input" 
					v-model="myFormData.name"
					maxlength="50"></input>
			</view>
			<view class="cu-form-group">
				<view class="title">性别</view>
				<switch class='switch-sex' 
					@change="SwitchC" 
					:class="switchC?'checked':''" 
					:checked="switchC?true:false"
					:color="'#E96141'"></switch>
			</view>
			<!-- <view class="cu-form-group margin-top">
				<view class="title">省市区县</view>
				<input placeholder="(非必填)" name="input" 
					v-model="myFormData.address"
					maxlength="50"></input>
			</view> -->
			<view class="cu-form-group">
				<view class="title">详细住址</view>
				<textarea placeholder="非必填，如：楼号、单元、楼层、门牌号等(100字以内)" 
					maxlength="100"
					:value="myFormData.addressDetail"
					:auto-size="{ minRows: 3, maxRows: 5 }"
					@input="addressDetailInput"
					style="border:1px solid #eeeeee; padding: 10upx;"/>
			</view>
			
			<view class="cu-form-group">
				<view class="title">身份证号</view>
				<input placeholder="(非必填)" name="input" @blur="getbirthday"
					v-model="myFormData.identity"
					maxlength="18"></input>
			</view>
				
			<view class="cu-form-group">
				<view class="title">出生日期</view>
				<view class="uni-list-cell-db">
					<picker mode="date" :value="myFormData.birthday" :start="startDate" :end="endDate" 
						@change="bindBirthdayDateChange">
						<view class="uni-input">{{myFormData.birthday?myFormData.birthday:'选择日期'}}</view>
					</picker>
				</view>
			</view>	
				
			<view class="cu-form-group">
				<view class="title">既往史</view>
				<textarea placeholder="(非必填)" name="input" 
					v-model="myFormData.jws"
					maxlength="1000"></textarea>
			</view>	
			
			<view class="cu-form-group">
				<view class="title">语言</view>
				<input placeholder="(非必填)" name="input" 
					v-model="myFormData.yuyan"
					maxlength="50"></input>
			</view>
			
			<view class="cu-form-group">
				<view class="title">过敏史</view>
				<textarea placeholder="(非必填)" name="input" 
					v-model="myFormData.gms"
					maxlength="500"></textarea>
			</view>
			
			<view class="cu-form-group">
				<view class="title">家族史</view>
				<textarea placeholder="(非必填)" name="input" 
					v-model="myFormData.jzs"
					maxlength="500"></textarea>
			</view>
			
			<view class="cu-form-group">
				<view class="title">体温</view>
				<input placeholder="(非必填)" name="input" 
					v-model="myFormData.tiwen"
					maxlength="50"></input>℃
			</view>
			
			<view class="cu-form-group">
				<view class="title">脉搏</view>
				<input placeholder="(非必填)" name="input" 
					v-model="myFormData.maibo"
					maxlength="50"></input>分/次
			</view>	
			
			<view class="cu-form-group">
				<view class="title">皮肤</view>
				<input placeholder="(非必填)" name="input" 
					v-model="myFormData.pifu"
					maxlength="50"></input>
			</view>
			<view class="cu-form-group">
				<view class="title">血压</view>
				<input placeholder="(非必填)" name="input" 
					v-model="myFormData.xueya"
					maxlength="50"></input>mmHg
			</view>
			
			<view class="cu-form-group">
				<view class="title">听力</view>
				<input placeholder="(非必填)" name="input" 
					v-model="myFormData.tingli"
					maxlength="50"></input>
			</view>
			
			<view class="cu-form-group">
				<view class="title">心肺听诊</view>
				<input placeholder="(非必填)" name="input" 
					v-model="myFormData.xinfeitingzhen"
					maxlength="50"></input>
			</view>
			<view class="cu-form-group">
				<view class="title">四肢活动</view>
				<input placeholder="(非必填)" name="input" 
					v-model="myFormData.sizhihl"
					maxlength="50"></input>
			</view>
			<view class="cu-form-group">
				<view class="title">心电图</view>
				<input placeholder="(非必填)" name="input" 
					v-model="myFormData.xindianjiance"
					maxlength="50"></input>
			</view>
			
			<view class="cu-form-group">
				<view class="title">血糖</view>
				<input placeholder="(非必填)" name="input" 
					v-model="myFormData.xuetang"
					maxlength="50"></input>mmol/L
			</view>
			
			<view class="cu-form-group">
				<view class="title">总胆固醇</view>
				<input placeholder="(非必填)" name="input" 
					v-model="myFormData.tgjc"
					maxlength="50"></input>mmol/L
			</view> 
			<view class="cu-form-group">
				<view class="title">尿常规</view>
				<input placeholder="(非必填)" name="input" 
					v-model="myFormData.jiayiliugan"
					maxlength="50"></input>
			</view>
			<view class="cu-form-group">
				<view class="title">尿酸</view>
				<input placeholder="(非必填)" name="input" 
					v-model="myFormData.niaosuan"
					maxlength="50"></input>umol/L
			</view>
			
			<view class="cu-form-group margin-top">
				<view class="title">大便隐血</view>
				<input placeholder="(非必填)" name="input" 
					v-model="myFormData.dabianyinxue"
					maxlength="50"></input>
			</view>
			
			<view class="cu-form-group">
				<view class="title">幽门螺旋杆菌</view>
				<input placeholder="(非必填)" name="input" 
					v-model="myFormData.ymlxgj"
					maxlength="50"></input>
			</view>
			
			<view class="cu-form-group">
				<view class="title">饮食指导</view>
				<input placeholder="(非必填)" name="input" 
					v-model="myFormData.yingshipinggu"
					maxlength="50"></input>
			</view>
			<!-- <view class="cu-form-group">
				<view class="title">腹部检查</view>
				<input placeholder="(非必填)" name="input" 
					v-model="myFormData.fubujiancha"
					maxlength="50"></input>
			</view> -->
			
			<!-- <view class="cu-form-group">
				<view class="title">呼吸</view>
				<input placeholder="(非必填)" name="input" 
					v-model="myFormData.huxi"
					maxlength="50"></input>
			</view> 
			<view class="cu-form-group">
				<view class="title">甲乙型流感</view>
				<input placeholder="(非必填)" name="input" 
					v-model="myFormData.jiayiliugan"
					maxlength="50"></input>
			</view>-->
			
					
			<!-- <view class="cu-form-group">
				<view class="title">内科</view>
				<input placeholder="(非必填)" name="input" 
					v-model="myFormData.neike"
					maxlength="50"></input>
			</view>	 -->
			
			
			
			<view class="cu-form-group">
				<view class="title">特殊处理</view>
				<input placeholder="(非必填)" name="input" 
					v-model="myFormData.teshuchuli"
					maxlength="50"></input>
			</view>
			<view class="cu-form-group">
				<view class="title">删除图片</view>
				<input placeholder="(输入数字,1是第一张)" name="input" 
					v-model="myFormData.fubujiancha"
					maxlength="50"></input>
			</view>
			<view class="cu-form-group" style="display: block;">
				<view class="title">化验单</view>
				<view class="bg-img" v-for="(item,index) in imagList" :key="index"  :data-url="imagList[index]">
					<image :src="imagList[index]" mode="aspectFill"></image>
				</view> 
			</view>
			<view style="width: 100%,height: 100%">
				<myImageUpload :value="uploadpicids" ></myImageUpload>
			</view>
			<!-- <view class="cu-form-group">
				<view class="title">血氧</view>
				<input placeholder="(非必填)" name="input" 
					v-model="myFormData.xueyang"
					maxlength="50"></input>
			</view> -->
			<!--<view class="cu-form-group">
				<view class="title">血脂</view>
				<input placeholder="(非必填)" name="input" 
					v-model="myFormData.xuezhi"
					maxlength="50"></input>
			</view>
			-->
			
			<view class="cu-form-group margin-top">
				<view class="title">医生签字</view>
				<input placeholder="(非必填)" name="input" 
					v-model="myFormData.doctor"
					maxlength="20"></input>
			</view>
			<view class="cu-form-group">
				<view class="title">护士签字</view>
				<input placeholder="(非必填)" name="input" 
					v-model="myFormData.nurse"
					maxlength="50"></input>
			</view>
			
			<view class="padding flex flex-direction bottom-btn-con">
				<button class="cu-btn bg-blue lg" 
					style="background-color: #E96141;" 
					@click="onSubmit()">保存</button>
			</view>
			
			
			
		</form>
	</view>
</template>

<script>
	import myImageUpload from '@/components/my-componets/my-image-upload.vue';
	export default {
		components:{
			myImageUpload
		 },
		name: "dnSingleMemberSave",
		data() {
			const currentDate = this.getDate({
				format: true
			})
			return {
				memberInfo: {
					id: '',
					phone: '',
					realname: '',
					sex: '',
					birthday: '',
					address: '',
				},
				jkdaId: '',			//健康档案ID(''表示新增, 非''表示修改)
				uploadpicids: '',   //健康档案ID，会员ID，用逗号分割
				imagList: [],
				switchC: true,
				myFormData:{
					name: '',
					sex: '',
					//address: '',
					addressDetail: '',
					birthday: '',
					identity: '',
					dabianyinxue: '',
					doctor: '',
					fubujiancha: '',
					gms: '',
					huxi: '',
					jiayiliugan: '',
					jws: '',
					jzs: '',
					maibo: '',
					neike: '',
					niaosuan: '',
					nurse: '',
					pifu: '',
					sizhihl: '',
					teshuchuli: '',
					tgjc: '',
					thxhdb: '',
					tingli: '',
					tiwen: '',
					xindianjiance: '',
					xinfeitingzhen: '',
					xuetang: '',
					xueya: '',
					xueyang: '',
					xuezhi: '',
					yingshipinggu: '',
					ymlxgj: '',
					yuyan: '',
					memberId: '',
				},
			};
		},
		created() {
			//console.log('save created this.jkdaId = ', this.jkdaId)
		},
		onLoad: function (option) {
			//console.log('save onLoad option = ', option)
			this.memberInfo.id = option.memId?option.memId:''
			this.memberInfo.phone = option.memPhone?option.memPhone:''
			this.memberInfo.realname = option.memRealname?option.memRealname:''
			this.memberInfo.sex = option.memSex?option.memSex:''
			this.memberInfo.birthday = option.memBirthday?option.memBirthday:''
			this.memberInfo.address = option.memAddress?option.memAddress:''
			
			this.myFormData.memberUserAccount = this.memberInfo.phone
			this.myFormData.birthday = this.memberInfo.birthday
			
			this.jkdaId = option.jkdaId?option.jkdaId:''
			this.uploadpicids = this.memberInfo.id +"," + this.jkdaId
			if(this.jkdaId){
				//健康档案有ID, 查询最新数据
				this.loadJkdaNewest()
				if(this.myFormData.thxhdb){
					this.imagList.push(this.myFormData.thxhdb.join(","))
				}
				console.info("00000000000:"+this.myFormData.thxhdb)
				console.info("00000000001:"+this.imagList)
			}else{
				//健康档案没ID, 表示新添加
				this.myFormData.name = this.memberInfo.realname
			}
		},
		computed: {
			startDate() {
				return this.getDate('start');
			},
			endDate() {
				return this.getDate('end');
			}
		},
		methods: {
			
			//详情地址填写
			addressDetailInput(e){
				let value = e.detail.value
				if(!value || value.length<1){
					this.myFormData.addressDetail = ''
				}
				this.myFormData.addressDetail = value
			},
			//备注填写
			addRemarkInput(e){
				let value = e.detail.value
				if(!value || value.length<1){
					this.myFormData.remark = ''
				}
				this.myFormData.remark = value
			},
			//选择出生日期
			bindBirthdayDateChange: function(e) {
				this.myFormData.birthday = e.target.value
			},
			//删除图片
			// delpic: function(e) {
			// 	console.info(e.target.value);
			// 	var delpicIndex = e.target.value;  //删除的图片下标
			// 		var birthday = "";
			// 		if (delpicIndex != null && delpicIndex != "") {
			// 			if (delpicIndex.length > 0) {
							
			// 			} else if (idCard.length == 18) {
			// 				birthday = idCard.substr(6, 8);
			// 			}
				
			// 			birthday = birthday.replace(/(.{4})(.{2})/, "$1-$2-");
			// 		}
			// 		console.info("==get====="+birthday);
			// 		this.myFormData.birthday =birthday;
			// },
			
			getbirthday: function(e) {
				console.info(e.target.value);
				var idCard = e.target.value;
					var birthday = "";
					if (idCard != null && idCard != "") {
						if (idCard.length == 15) {
							birthday = "19" + idCard.substr(6, 6);
						} else if (idCard.length == 18) {
							birthday = idCard.substr(6, 8);
						}
				
						birthday = birthday.replace(/(.{4})(.{2})/, "$1-$2-");
					}
					console.info("==get====="+birthday);
					this.myFormData.birthday =birthday;
			},
			
			getDate(type) {
				const date = new Date();
				let year = date.getFullYear();
				let month = date.getMonth() + 1;
				let day = date.getDate();
	
				if (type === 'start') {
					year = year - 130;
				} else if (type === 'end') {
					year = year;
				}
				month = month > 9 ? month : '0' + month;
				day = day > 9 ? day : '0' + day;
				
				console.info("===22===="+this.myFormData);
				return `${year}-${month}-${day}`;
			},
			

			//按会员健康档案ID, 加载最新信息
			loadJkdaNewest(){
				uni.showLoading({
					title:'加载中...',
					mask:true
				})
				let _params = {
					id: this.jkdaId
				}
				let that = this
				that.$http.get("/health/jkda/queryById",{params:_params}).then(res=>{
					if(res.data.success){
						uni.hideLoading()
						let jsonData = res.data.result
						that.myFormData = jsonData
						this.imagList = this.myFormData.thxhdb.split(",")
						if(jsonData.sex=='1'){
						  that.switchC = true
						}else{
						  that.switchC = false
						}
					}else{
						uni.hideLoading()
					}
				}).catch(err => {
					uni.hideLoading()
				});
			},
			//返回此会员的健康档案列表
			backThisMemberJkdaList(){
				let _params = '?nouse=1' +
					'&memId=' + this.memberInfo.id + 
					'&memPhone=' + this.memberInfo.phone + 
					'&memRealname=' + this.memberInfo.realname + 
					'&memSex=' + this.memberInfo.sex + 
					'&memBirthday=' + this.memberInfo.birthday + 
					'&memAddress=' + this.memberInfo.address
				//console.log('save _params = ', _params)
				uni.redirectTo({
					url: '/pages/member-health-record/dn-single-member-see' + _params
				})
			},
			//提交保存
			onSubmit() {
				if(this.jkdaId){
					this.modifyData()
				}else{
					this.addData()
				}
			},
			//新增
			addData(){
				uni.showLoading({
					title: '处理中..',
					mask: true,
				})
				let that = this
				that.myFormData.memberId = this.memberInfo.id
				that.$http.post("/health/jkda/add",that.myFormData).then(res=>{
					if(res.data.success){
						uni.hideLoading()
						that.$tip.success('添加成功')
						setTimeout(function() {
							/* uni.navigateBack({
								delta: 1
							}) */
							that.backThisMemberJkdaList()
						}, 500);
					}else{
						uni.hideLoading()
						that.$tip.toast('添加失败');	
					}
				}).catch(err => {
					uni.hideLoading()
					that.$tip.error('添加失败')
				});
			},
			//修改
			modifyData(){
				uni.showLoading({
					title: '处理中..',
					mask: true,
				})
				let that = this
				that.myFormData.id = this.jkdaId
				that.$http.put("/health/jkda/edit",that.myFormData).then(res=>{
					if(res.data.success){
						uni.hideLoading()
						that.$tip.success('保存成功')
						setTimeout(function() {
							/* uni.navigateBack({
								delta: 1
							}) */
							that.backThisMemberJkdaList()
						}, 500);
					}else{
						uni.hideLoading()
						that.$tip.toast('保存失败');	
					}
				}).catch(err => {
					uni.hideLoading()
					that.$tip.error('保存失败')
				});
			},
			//切换性别
			SwitchC(e) {
				this.switchC = e.detail.value
				if(this.switchC){
					this.myFormData.sex = '1'
				}else{
					this.myFormData.sex = '2'
				}
			},
		}
	}

	
</script>

<style>
	.main-con{
		height: auto;
		overflow: hidden;
		border: 0px solid #f00;
		padding-bottom: 170upx;
	}
	.cu-form-group .title {
		min-width: calc(4em + 15px);
	}
	.bottom-btn-con{
		width: 100%;
		position: fixed; 
		bottom: 0; 
		background-color: #ffffff;
		box-shadow: 0 -1px 5px rgba(0, 0, 0, 0.06);
	}
</style>
